/*******************************************************************************
 * Package: com.hngtrust.iir.user.controller
 * Type:    MessageController
 * Date:    2021-6-7 19:52
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.user.controller;

import com.hngtrust.framework.bind.ResponseInfo;
import com.hngtrust.framework.page.PageResult;
import com.hngtrust.framework.page.service.PageService;
import com.hngtrust.iir.dto.ReqDTO;
import com.hngtrust.iir.risk.entity.Message;
import com.hngtrust.iir.risk.service.MessageService;
import com.hngtrust.iir.util.ListUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 消息处理
 * @author Chentianyong
 * @date 2021-6-7 19:52
 */
@Slf4j
@Api(tags = "消息管理")
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;
    
    /**
     * 分页服务
     */
    @Autowired
    private PageService pageService;
    
    /**
     * 消息列表
     */
    @ApiOperation(value = "消息列表", notes = "消息列表")
    @PostMapping("/list-message")
    public ResponseInfo<PageResult<Message>> listMessage(
            @ApiParam(value = "消息条件") @Valid @RequestBody ReqDTO<Message> req) {
        log.info("MessageController.listMessage start...");
    
        Message message = new Message();
        BeanUtils.copyProperties(req.getContent(),message);
        PageResult<Message> page = pageService.page(req.getPageParam(), () -> messageService.queryList(message));
    
        PageResult<Message> pageResult = new PageResult<>();
        ListUtil<Message> listUtil = BeanUtils.instantiateClass(ListUtil.class);
        listUtil.copyList(page,pageResult,Message.class);
        
        return new ResponseInfo(pageResult);
    }
    
    /**
     * 消息设置
     */
    @ApiOperation(value = "消息设置", notes = "消息设置")
    @PostMapping("/set-message")
    public ResponseInfo<Object> setMessage(
            @ApiParam(value = "消息条件") @Valid @RequestBody Message req) {
        log.info("MessageController.setMessage start...");
    
//        if(!(req.getUserId() > 0L)){
//            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"UserID");
//        }
//        if(!(req.getType() > 0)){
//            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"Type");
//        }
        
        messageService.updateStatus(req);
    
        return new ResponseInfo<>();
    }
}
